/**
 * file
 */
package com.tdkj.action.front.member.spaceManagement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

//import org.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.enterprisezone.BeanEnterprise;
import com.tdkj.model.enterprisezone.BeanEnterpriseProductClass;
import com.tdkj.model.enterprisezone.BeanEnterpriseZone;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.enterprisezone.IBeanEnterpriseProductClassService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseService;
import com.tdkj.util.PublicConfing;

/**

 * FileName:     ContactAction.java

 * @Description: TODO联系方式

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-27 上午11:48:33

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-27       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class AjaxEnterpriseZoneProductClassManage extends BaseAction {
	
	private String result ;
	private IBeanEnterpriseService enterpriseService;
	private IBeanEnterpriseProductClassService enterpriseProductClassService;
	
	@Override
	public String execute(){
		return "success";
	}
	
	public String classData(){
		BeanUser user =  (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanEnterprise enterprise = user.getEnterprise();
		enterprise = enterpriseService.findById(enterprise.getId(), BeanEnterprise.class);

		BeanEnterpriseProductClass productClassCondition = new BeanEnterpriseProductClass();
		productClassCondition.setEnterprise(enterprise);
		productClassCondition.setIsAvailable(1);
		productClassCondition.setParentClass(null);
		
		List productClassList = enterpriseProductClassService.findByExample(productClassCondition);
		for(int i = 0 ; i < productClassList.size(); i ++){
			BeanEnterpriseProductClass productClass = (BeanEnterpriseProductClass) productClassList.get(i);
			if(productClass.getParentClass() != null){
				productClassList.remove(productClass);
			}
		}
		if(0 == productClassList.size()){
			result =  "[{\"id\":\"-1\",\"text\":\"产品分类\" }]";
		}else{
			//JSONObject jsonresult = new JSONObject(productClassList);
			JsonConfig jsonConfig = new JsonConfig();  
			//jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); 
			jsonConfig.setExcludes(new String[] {// 只要设置这个数组，指定过滤哪些字段。
					"enterprise", "isAvailable","parentClass","remark","description"});
			JSONArray jsonresult = JSONArray.fromObject(productClassList, jsonConfig);
			//result= "[{\"id\":\"root\",\"text\":\"root\" ,\"children\":[{\"id\":\"1\",\"text\":\"产品分类1\"}]}]";
			result = jsonresult.toString();
			result = result.replaceAll("name", "text");
			result = result.replaceAll("subClass", "children");
			result = "[{\"id\":\"-1\" , \"text\":\"产品分类\" ,\"children\":" + result + "}]";
			System.out.print(result);
		}
		return "success";
	}
	 
	public String addClass(){
		Long parentClassId = null;
		String className = null;
		BeanEnterpriseProductClass parentClass = null;
		if(ServletActionContext.getRequest().getParameter("parentclassid") != "" ){
			parentClassId =  (Long) Long.valueOf( ServletActionContext.getRequest().getParameter("parentclassid"));
		}
		if(ServletActionContext.getRequest().getParameter("classname") != ""  ){
			className = ServletActionContext.getRequest().getParameter("classname");
		}
		BeanEnterpriseProductClass enterpriseZoneProductClass = new BeanEnterpriseProductClass();
		
		BeanUser user =  (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanEnterprise enterprise = user.getEnterprise();
		enterprise = enterpriseService.findById(enterprise.getId(), BeanEnterprise.class);
		
		/*企业不存在,不能添加*/
		if(null != enterprise){
			enterpriseZoneProductClass.setEnterprise(enterprise);
		}else{
			result = "error";
			return "success";
		}

		if(null != className){
			enterpriseZoneProductClass.setName(className);
		}else{
			result = "error";
			return "success";
		}
		
		if(null != parentClassId && -1 != parentClassId){
			parentClass = enterpriseProductClassService.findById(parentClassId, BeanEnterpriseProductClass.class);
			enterpriseZoneProductClass.setParentClass(parentClass);
		}
		
		try{
			enterpriseProductClassService.save(enterpriseZoneProductClass);
			result = enterpriseZoneProductClass.getId().toString();
		}catch(Exception exception){
			result = "error";
		}
		return execute();
	}
	
	public String deleteClass(){
		Long classid = (Long) Long.valueOf( ServletActionContext.getRequest().getParameter("classid"));
		try{
			BeanEnterpriseProductClass productClass =  enterpriseProductClassService.findById(classid , BeanEnterpriseProductClass.class);
			BeanEnterpriseProductClass parentClass = productClass.getParentClass();
			productClass.setIsAvailable(0);
			enterpriseProductClassService.saveOrUpdate(productClass, BeanEnterpriseProductClass.class);
			if(parentClass != null){
				parentClass.getSubClass().remove(productClass);
				enterpriseProductClassService.saveOrUpdate(parentClass , BeanEnterpriseProductClass.class);
			}
			result = "success";
		}catch(Exception exception){
			result = "error";
		}
		return "success";
	}
	
	public String editClass(){
		Long classid = (Long) Long.valueOf( ServletActionContext.getRequest().getParameter("classid"));
		String	className =  ServletActionContext.getRequest().getParameter("classname");
		BeanEnterpriseProductClass enterpriseZoneProductClass = enterpriseProductClassService.findById(classid, BeanEnterpriseProductClass.class);
		if(null == enterpriseZoneProductClass){
			result = "error";
		}else{
			enterpriseZoneProductClass.setName(className);			
		}
		
		try{
			enterpriseProductClassService.saveOrUpdate(enterpriseZoneProductClass, BeanEnterpriseProductClass.class);
			result = "success";
		}catch(Exception exception){
			result = "error";
		}		
		return "success";
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Resource
	public void setEnterpriseProductClassService(
			IBeanEnterpriseProductClassService enterpriseZoneProductClassService) {
		this.enterpriseProductClassService = enterpriseZoneProductClassService;
	}

	@Resource
	public void setEnterpriseService(IBeanEnterpriseService enterpriseService) {
		this.enterpriseService = enterpriseService;
	}
	
}
